package com.shiliao.app.activity.index.presenter;

import android.text.TextUtils;

import com.shiliao.app.ShiliaoApplication;
import com.shiliao.app.activity.index.entity.WaitExamineEntity;
import com.shiliao.app.activity.index.service.WaitExaminDetailService;
import com.shiliao.app.activity.index.service.impl.WaitExamineDeatilImpl;
import com.shiliao.app.activity.index.view.WaitExaminDetailView;
import com.shiliao.app.utils.YMServerUtil;
import com.shiliao.app.weight.FileUtils;
import com.yimei.framework.mvp.presenter.IBasePresenter;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Created by yinzhiqiang on 2018/4/22.
 */

public class WaitExamineDetailPresenter extends IBasePresenter<WaitExaminDetailService>{
    private WaitExaminDetailView waitExaminDetailView;
    private WaitExaminDetailService waitExaminDetailService;

    public WaitExamineDetailPresenter(WaitExaminDetailView view) {
        waitExaminDetailView = view;
        waitExaminDetailService=new WaitExamineDeatilImpl();
        setService(waitExaminDetailService);
    }

    public void requestExaminDeatil(String userId,String id,String type){
        Map map=new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(id)) {
            map.put("id", id);
        }
        if(!TextUtils.isEmpty(type)) {
            map.put("type", type);
        }
        waitExaminDetailService.requestExamineDeatil(YMServerUtil.ACTION_GET_WAITEXMINE_DETAIL, map,
                new ICallBack() {
                    @Override
                    public void onSuccess(int statusCode, Object result) {
                        waitExaminDetailView.getContentText(result.toString());
                    }

                    @Override
                    public void onFail(int statusCode, Object result) {
                        waitExaminDetailView.getContentText(result.toString());
                    }
                });
    }

    public void requestAuditResult(String userId,String id,String type,String result){
        Map map=new HashMap();
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        if (!TextUtils.isEmpty(id)) {
            map.put("id", id);
        }
        if(!TextUtils.isEmpty(type)) {
            map.put("type", type);
        }
            map.put("result", result);

        waitExaminDetailService.requestExamineDeatil(YMServerUtil.ACTION_AUDIT_RESULT, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
               waitExaminDetailView.sendBordCast();
               waitExaminDetailView.commitSuccess();
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }

    public void getAuditResult(){
        Map map=new HashMap();
        waitExaminDetailService.requestExamineDeatil(YMServerUtil.ACTION_GET_AUDIT_RESULT, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                try {
                    JSONObject jsonObject=new JSONObject(result.toString());
                    Iterator iterator=jsonObject.keys();
                    List<WaitExamineEntity> list=new ArrayList<>();
                    while (iterator.hasNext()){
                        WaitExamineEntity entity=new WaitExamineEntity();
                        entity.setExaminId(iterator.next() + "");
                        entity.setExaminResult(jsonObject.get(entity.examinId));
//                        entity.examinId = iterator.next() + "";
//                        entity.examinResult=  jsonObject.get(entity.examinId);
                        list.add(entity);

                    }
                    Collections.sort(list,new WaitExamineEntity());
                    waitExaminDetailView.getExaminResultList(list);
//                    for (int i=0;i<list.size();i++){
//                        String id= list.get(i).examinId;
//                        String examinResult=list.get(i).examinResult.toString();
//                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFail(int statusCode, Object result) {
                LogUtil.e("yzz",result.toString());
            }
        });
    }

    public void requestShare(String articleId, String userId) {//请求分享内容
        Map map = new HashMap();
        if (!TextUtils.isEmpty(articleId)) {
            map.put("article_id", articleId);
        }
        if (!TextUtils.isEmpty(userId)) {
            map.put("user_id", userId);
        }
        waitExaminDetailService.requestExamineDeatil(YMServerUtil.ACTION_SHARE, map, new ICallBack() {
            @Override
            public void onSuccess(int statusCode, Object result) {
                LogUtil.e("yzq", result.toString());
                String sharTitle = ParseHelper.getString(result.toString(), "title");
                String sharDescription = ParseHelper.getString(result.toString(), "description");
                String sharPicurl = ParseHelper.getString(result.toString(), "pic_url");
                String sharurl = ParseHelper.getString(result.toString(), "url");
                waitExaminDetailView.shareContent(sharTitle, sharPicurl, sharurl, sharDescription);
            }

            @Override
            public void onFail(int statusCode, Object result) {

            }
        });
    }
}
